package com.wkbb.basic.controller.open;

import com.github.pagehelper.PageInfo;
import com.wkbb.basic.model.Employee;
import com.wkbb.basic.service.EmployeeService;
import com.wkbb.basic.service.TeacherManageService;
import com.wkbb.basic.service.UserOpenService;
import com.wkbb.common.constants.Constants;
import com.wkbb.common.constants.ErrorCode;
import com.wkbb.common.constants.VersionConstant;
import com.wkbb.common.dto.ResultDTO;
import com.wkbb.common.dto.basic.EmployeeInfoDto;
import com.wkbb.common.dto.im.SchoolAddressBookDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 基础模块对外接口
 */
@RestController
@Api(description = "职员模块对外接口")
@RequestMapping("/basic/{apiVersion}/open")
@Slf4j
public class EmployeeController {

    @Autowired
    private UserOpenService userOpenService;

    @Autowired
    private TeacherManageService teacherManageService;

    @Autowired
    private EmployeeService employeeService;

    @ApiOperation(value = "查询员工详情")
    @RequestMapping(value = "/getEmployeeInfo", method = RequestMethod.GET)
    public ResultDTO<EmployeeInfoDto> getEmployeeInfo(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion") String apiVersion,
            @ApiParam(value = "学校ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId,
            @ApiParam(value = "员工id", required = true) @RequestParam(name = "userId", required = true) Long userId) {
        log.info("***********EmployeeController.getEmployeeInfo******orgId:{} userId:{}", orgId, userId);

        try {

            EmployeeInfoDto employeeInfoDto = employeeService.getEmployeeInfo(orgId,userId);
            return new ResultDTO<EmployeeInfoDto>(employeeInfoDto);
        } catch (Exception e) {
            log.error("***********EmployeeController.getEmployeeInfo******", e);
            return new ResultDTO<>(false);
        }
    }

    @ApiOperation(value = "根据组织ID获取职员列表")
    @RequestMapping(value = "getEmployeeInfoList", method = RequestMethod.GET)
    public ResultDTO<List<EmployeeInfoDto>> getEmployeeInfoList(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "学校ID", required = true, defaultValue = "1") @RequestParam(name = "orgId", required = true) Long orgId) {
        log.info("***********EmployeeController.getEmployeeInfoList******apiVersion:{},orgId:{},classId:{}", apiVersion, orgId);

        try {
            if (orgId == 0) {
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }

            Map<String, Object> params = new HashMap<>(2);
            params.put("orgId", orgId);

            return new ResultDTO<List<EmployeeInfoDto>>(teacherManageService.getEmployeeInfoList(params));
        } catch (Exception e) {
            log.error("***********EmployeeController.getEmployeeInfoList******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "根据班级ID获取任课教师列表")
    @RequestMapping(value = "getEmployeeInfoListByClassId", method = RequestMethod.GET)
    public ResultDTO<List<EmployeeInfoDto>> getEmployeeInfoListByClassId(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "学校ID", required = true, defaultValue = "1") @RequestParam(name = "orgId", required = true) Long orgId,
            @ApiParam(value = "班级ID", required = true, defaultValue = "1") @RequestParam(name = "classId", required = true) Long classId) {
        log.info("***********EmployeeController.getEmployeeInfoListByClassId******apiVersion:{},orgId:{},classId:{}", apiVersion, orgId, classId);

        try {
            if (orgId == 0 || classId == 0) {
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }

            Map<String, Object> params = new HashMap<>(2);
            params.put("orgId", orgId);
            params.put("classId", classId);

            return new ResultDTO<>(teacherManageService.getEmployeeInfoListByClassId(params));
        } catch (Exception e) {
            log.error("***********EmployeeController.getEmployeeInfoListByClassId******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "根据班级查询老师列表(包含班主任)")
    @RequestMapping(value = "getEmployeeInfoClass", method = RequestMethod.GET)
    public ResultDTO<List<EmployeeInfoDto>> getEmployeeInfoClass(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "学校ID", required = true, defaultValue = "1") @RequestParam(name = "orgId", required = true) Long orgId,
            @ApiParam(value = "班级ID", required = true, defaultValue = "1") @RequestParam(name = "classId", required = true) Long classId) {
        log.info("***********EmployeeController.getEmployeeInfoByClass******apiVersion:{},orgId:{},classId:{}", apiVersion, orgId, classId);

        try {
            if (orgId == 0 || classId == 0) {
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }

            Map<String, Object> params = new HashMap<>(2);
            params.put("orgId", orgId);
            params.put("classId", classId);

            return new ResultDTO<>(teacherManageService.getEmployeeInfoByClass(params));
        } catch (Exception e) {
            log.error("***********EmployeeController.getEmployeeInfoByClass******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "根据班级查询老师列表(包含园长)")
    @RequestMapping(value = "getEmployeeListBySchoolMaster", method = RequestMethod.GET)
    public ResultDTO<List<EmployeeInfoDto>> getEmployeeListBySchoolMaster(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "学校ID", required = true, defaultValue = "1") @RequestParam(name = "orgId", required = true) Long orgId,
            @ApiParam(value = "班级ID", required = true, defaultValue = "1") @RequestParam(name = "classId", required = true) Long classId) {
        log.info("***********EmployeeController.getEmployeeListBySchoolMaster******apiVersion:{},orgId:{},classId:{}", apiVersion, orgId, classId);

        try {
            if (orgId == 0 || classId == 0) {
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }

            Map<String, Object> params = new HashMap<>(2);
            params.put("orgId", orgId);
            params.put("classId", classId);

            return new ResultDTO<>(teacherManageService.getEmployeeListBySchoolMaster(params));
        } catch (Exception e) {
            log.error("***********EmployeeController.getEmployeeListBySchoolMaster******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "老师或园长的通讯录")
    @RequestMapping(value = "getFacultyAddressBook", method = RequestMethod.GET)
    public ResultDTO<SchoolAddressBookDto> getFacultyAddressBook(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "学校ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId,
            @ApiParam(value = "用户ID", required = false) @RequestParam(name = "userId", required = false) Long userId,
            @ApiParam(value = "查询类型(2:家长通讯录,3:老师通讯录,1园长通讯录)", required = true) @RequestParam(name = "type", required = true) Integer type) {
        log.info("***********EmployeeController.getFacultyAddressBook******apiVersion:{},userId:{},orgId:{},type{}", apiVersion, userId, orgId, type);
        try {
            if (orgId == 0 || userId == 0 || type == 0) {
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }
            return new ResultDTO<>(userOpenService.getFacultyAddressBook(orgId, userId, type));
        } catch (Exception e) {
            log.error("***********EmployeeController.getFamilyAddressBook******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }


    @ApiOperation(value = "多条件查询老师分页列表")
    @RequestMapping(value = "/getTeacherPageList", method = RequestMethod.GET)
    public ResultDTO<PageInfo<EmployeeInfoDto>> getTeacherPageList(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion") String apiVersion,
            @ApiParam(value = "组织ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId,
            @ApiParam(value = "岗位ID",required = false) @RequestParam(name = "positionId", required = false) Long positionId,
            @ApiParam(value = "姓名",required = false) @RequestParam(name = "name", required = false) String name,
            @ApiParam(value = "手机号",required = false) @RequestParam(name = "mobile", required = false) String mobile,
            @ApiParam(value = "性别",required = false) @RequestParam(name = "sex", required = false) String sex,
            @ApiParam(value = "姓名/手机号",required = false) @RequestParam(name = "searchName", required = false) String searchName,
            @ApiParam(value = "职务/岗位: 1园长 2教师 3保育员 4 营养师 5代课教师 6其他", defaultValue = "2", required = false) @RequestParam(name = "userType", required = false) Short userType,
            @ApiParam(value = "页码", required = false) @RequestParam(name = "pageNo", required = false, defaultValue = "1") Integer pageNo,
            @ApiParam(value = "每页条数", required = false) @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        try {
            HashMap<String, Object> params = new HashMap<>(Constants.INIT_MAP_SIZE_10);
            params.put("orgId", orgId);
            params.put("positionId", positionId);
            params.put("name", name);
            params.put("userType", userType);
            params.put("mobile", mobile);
            params.put("sex", sex);
            params.put("searchName", searchName);
            return new ResultDTO<>(userOpenService.getTeacherPageList(params,pageNo,pageSize));
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultDTO<>(false);
        }
    }

    @ApiOperation(value = "修复班主任没群接口")
    @RequestMapping(value = "/saveImGroupByMasterId", method = RequestMethod.GET)
    public ResultDTO<Object> saveImGroupByMasterId() {
        log.info("***********EmployeeController.saveImGroupByMasterId******");
        try {
            userOpenService.saveImGroupByMasterId();
            return new ResultDTO<Object>();
        } catch (Exception e) {
            log.error("***********EmployeeController.getFamilyAddressBook******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }



    @ApiOperation(value = "查询园长用户列表")
    @RequestMapping(value = "/getMasterEmployeeList", method = RequestMethod.GET)
    public ResultDTO<List<EmployeeInfoDto>> getMasterEmployeeList(
        @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion") String apiVersion,
        @ApiParam(value = "组织ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId
    ) {
        log.info("***********EmployeeController.getMasterEmployeeList******");
        try {
            Map<String,Object> params = new HashMap<>(Constants.INIT_MAP_SIZE_5);
            params.put("orgId",orgId);
            return new ResultDTO<>(teacherManageService.getMasterEmployeeList(params));
        } catch (Exception e) {
            log.error("***********EmployeeController.getMasterEmployeeList******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "根据班级ID和学校ID查询班主任信息")
    @RequestMapping(value = "/getClassMasterInfo", method = RequestMethod.GET)
    public ResultDTO<EmployeeInfoDto> getClassMasterInfo(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "学校ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId,
            @ApiParam(value = "班级ID", required = true) @RequestParam(name = "classId", required = true) Long classId

    ){
        try{
            log.info("***********EmployeeController.getClassMasterInfo******orgId:{},classId:{}",orgId,classId);
            //在此验证必传参数
            if(null==orgId||null==classId){
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }

            return new ResultDTO<>(teacherManageService.getClassMasterInfo(orgId,classId));
        }catch (Exception e){
            log.error("*************ClassInfoController.getClassMasterInfo*************异常:{}", e);
            return new ResultDTO(false);
        }
    }

    @ApiOperation(value = "根据工号查询教师信息")
    @RequestMapping(value = "/getEmployeeByTeacherNo", method = RequestMethod.GET)
    public ResultDTO<EmployeeInfoDto> getEmployeeByTeacherNo(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "学校ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId,
            @ApiParam(value = "工号", required =true) @RequestParam(name = "teacherNo", required = true) String teacherNo){

        log.info("getEmployeeByTeacherNo---orgId:{},teacherNo:{}",orgId,teacherNo);

        try {
            Map<String,Object> params = new HashMap<>();
            params.put("orgId", orgId);
            params.put("jobNo", teacherNo);

            EmployeeInfoDto employeeInfoDto = employeeService.getEmployee(params);

            return new ResultDTO<EmployeeInfoDto>(employeeInfoDto);
        } catch (Exception e) {
            log.error("***********getEmployeeByTeacherNo******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "根据手机号获取教师接口")
    @RequestMapping(value = "/getEmployeeByMobile", method = RequestMethod.GET)
    public ResultDTO<EmployeeInfoDto> getEmployeeByMobile(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "学校ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId,
            @ApiParam(value = "手机号", required = true) @RequestParam(name = "mobile", required = true) String mobile){

        log.info("getEmployeeByMobile---orgId:{},mobile:{}",orgId,mobile);

        try {
            Map<String,Object> params = new HashMap<>();
            params.put("orgId", orgId);
            params.put("mobile", mobile);

            EmployeeInfoDto employeeInfoDto = employeeService.getEmployee(params);

            return new ResultDTO<EmployeeInfoDto>(employeeInfoDto);
        } catch (Exception e) {
            log.error("***********getEmployeeByMobile******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }
}
